//
//  searchModel.m
//  RouteSearcher
//
//  Created by wenqingzhou on 2/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SearchModel.h"

#define REQUEST_URL @"http://api.reittiopas.fi/public-ytv/fi/api/?user=wenqing.alex.zhou&pass=sigsg552"

@implementation searchModel
@synthesize fromXCode,fromYCode,toYCode,toXCode,time,timeType,date,tmp,arrayOfFromAddress,arrayOfToAddress,fromOrTo;

+(searchModel *)initWithString:(NSString *)itimeType
{
	searchModel *mySearchModel= [[searchModel alloc] init];
	mySearchModel.timeType=itimeType;
	return [mySearchModel autorelease];
}

-(NSString *)getAddressFromTmpArrayObjectAtIndex:(NSInteger)index
{

	return [[self.tmp objectAtIndex:index] formattedAddressFromGeocode];	
}
-(NSArray *)getFirstAddressFromTmpArrayObjectAtIndex:(NSInteger)index
{	
			return [NSArray arrayWithObjects:
					[[self.tmp objectAtIndex:index] xcode],
					[[self.tmp objectAtIndex:index] ycode],nil];
}

-(void)stringToGeocode:(NSString *)str
{
	NSURL *url=[NSURL URLWithString:[REQUEST_URL stringByAppendingFormat:@"&key=%@",str]];
	NSLog(@"%@",url);
	NSMutableURLRequest *request=[[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
	[request setHTTPMethod:@"GET"];
	NSHTTPURLResponse *response;
	NSData *data= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
	NSXMLParser *myXMLParser= [[NSXMLParser alloc] initWithData:data];
	myXMLParser.delegate =self;
	BOOL succ = [myXMLParser parse];
	[myXMLParser release];
	NSLog(@"succ=%d",succ);
}

-(void)parser:(NSXMLParser *)parser 
		didStartElement:(NSString *)elementName 
		namespaceURI:(NSString *)namespaceURI 
		qualifiedName:(NSString *)qualifiedName 
		attributes:(NSDictionary *)attributeDict
{
	if([elementName isEqualToString:@"LOC"])
	{
		[self.tmp addObject:[geocodeModel initWithAtrributes:attributeDict]];
	}
	else if([elementName isEqualToString:@"GEOCODE"])
	{		
		if(![self.tmp isEqual:nil])[self.tmp release];
		self.tmp = [[NSMutableArray alloc] init];
	}
}

-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qualifiedName 
{
	;
}

-(NSURL *)getRequestURLOfRoute
{
	if(self.toXCode&&self.toYCode&&self.fromXCode&&self.fromYCode)
	{
		NSString *tmpURL = [REQUEST_URL stringByAppendingFormat:@"&a=%@,%@&b=%@,%@&time=%@&date=%@&timemode=%@",self.fromXCode,self.fromYCode,self.toXCode,self.toYCode,self.time,self.date,self.timeType];
		NSLog(@"%@",tmpURL);
		return [[[NSURL alloc] initWithString:tmpURL] autorelease];
	}
	else {
		return nil;
	}

}

- (NSString *)updateDate:(NSDate *)myDate
{
	NSDateFormatter *df= [[NSDateFormatter alloc] init];
	[df setDateStyle:NSDateFormatterShortStyle];
	[df setTimeStyle:NSDateFormatterShortStyle];
	NSDateFormatter *df1= [[NSDateFormatter alloc] init];
	[df1 setDateFormat:@"yyyyMMdd"];
	self.date=[[[NSString alloc] initWithFormat:@"%@",[df1 stringFromDate:myDate]] autorelease];
	NSDateFormatter *df2= [[NSDateFormatter alloc] init];
	[df2 setDateFormat:@"HHmm"];
	self.time=[[[NSString alloc] initWithFormat:@"%@",[df2 stringFromDate:myDate]] autorelease];						 
	[df2 release];
	[df1 release];
	[df autorelease];
	return [[[NSString alloc] initWithFormat:@"%@",[df stringFromDate:myDate]] autorelease];
}

- (void)dealloc
{
	[self.fromXCode release];
	[self.fromYCode release];
	[self.toXCode release];
	[self.toYCode release];
	[self.time release];
	[self.date release];
	[self.timeType release];
	[self.tmp release];
	[self.arrayOfFromAddress release];
	[self.arrayOfToAddress release];
	[super dealloc];
}

@end
